﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace PokeTD
{
    class TowerEffectParticle
    {
        public Vector2 Position;
        public Vector2 Velocity;
        public float Lifetime;
        public Texture2D Texture;
        public Creep Creep;

        public TowerEffectParticle(Vector2 pos, Vector2 vel, float time, Texture2D tex, Creep creep = null)
        {
            Position = pos;
            Velocity = vel;
            Lifetime = time;
            Texture = tex;
            Creep = creep;
        }
    }

    class TowerEffect
    {
        protected float LifeTime;
        protected Creep Creep;
        protected List<TowerEffectParticle> Particles;
        protected static Random Random = new Random();

        public TowerEffect(float lifeTime, Creep creep)
        {
            LifeTime = lifeTime;
            Creep = creep;
            Particles = new List<TowerEffectParticle>();
        }

        public virtual void Update(GameTime gameTime)
        {
            LifeTime -= (float)gameTime.ElapsedGameTime.TotalSeconds;

            foreach (TowerEffectParticle p in Particles)
            {
                p.Position += p.Velocity * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
        }

        public virtual void Draw(SpriteBatch spriteBatch, Vector2 renderRoot)
        {
            if (Alive)
            {
                foreach (TowerEffectParticle p in Particles)
                {
                    spriteBatch.Draw(p.Texture, p.Position + p.Creep.Position + renderRoot, Color.White);
                }
            }
        }

        public bool Alive
        {
            get { return (!Creep.isDead && LifeTime > 0.0f); }
        }

        public static TowerEffect Get(string name, Creep creep, float lifeTime)
        {
            switch (name)
            {
                case "creep.stun": return new TowerEffectStun(lifeTime, creep);
                case "creep.slow": return new TowerEffectSlowed(lifeTime, creep);
                case "creep.burn": return new TowerEffectBurn(lifeTime, creep);
            }
            return null;
        }
    }
}
